﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SYSoft.Comm;
using SYSoft.Comm.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using WebSocketSharp.Server;

namespace SYSoft.WebSocketHelper
{
    /// <summary>
    /// 启动 websocket 服务器
    /// </summary>
    public class WebSocketService
    {
        WebSocketServer webserver;
        public WebSocketService()
        {
            webserver = new WebSocketServer(8082);
            webserver.AddWebSocketService<DeviceIOChart>("/MES1");
            //webserver.AddWebSocketService<DeviceIOChart>("/MES2");
            webserver.Start();

            LoadSwitch();

            ////采集程序
            //Task.Run(new Action(() =>
            //{
            //    WebSocketServiceHost serviceHost;
            //    webserver.WebSocketServices.TryGetServiceHost("/MES2", out serviceHost);
            //    while (true)
            //    {
            //        Thread.Sleep(1000);
            //        if (serviceHost.Sessions.Sessions.Count() > 0)
            //        {
            //            Console.WriteLine(serviceHost.Path+serviceHost.Sessions.Sessions.Count());
            //            serviceHost.Sessions.Broadcast("MES2");
            //        }
            //    }
            //}));
        }

        Dictionary<int, List<SwitchModel>> SwitchStatusList = new Dictionary<int, List<SwitchModel>>();


        private void LoadSwitch()
        {
            SwitchStatusList.Add(1001, new List<SwitchModel>());
            SwitchStatusList.Add(1002, new List<SwitchModel>());
            SwitchStatusList.Add(1003, new List<SwitchModel>());
            SwitchStatusList.Add(1004, new List<SwitchModel>());
            SwitchStatusList.Add(1005, new List<SwitchModel>());
            SwitchStatusList.Add(1006, new List<SwitchModel>());
            SwitchStatusList.Add(1007, new List<SwitchModel>());
            SwitchStatusList.Add(1008, new List<SwitchModel>());
            SwitchStatusList.Add(1009, new List<SwitchModel>());
            SwitchStatusList.Add(1010, new List<SwitchModel>());
            SwitchStatusList.Add(1011, new List<SwitchModel>());
            SwitchStatusList.Add(1012, new List<SwitchModel>());
            SwitchStatusList.Add(1013, new List<SwitchModel>());
            SwitchStatusList.Add(1014, new List<SwitchModel>());
            SwitchStatusList.Add(1015, new List<SwitchModel>());
            SwitchStatusList.Add(1016, new List<SwitchModel>());
            SwitchStatusList.Add(1017, new List<SwitchModel>());
            SwitchStatusList.Add(1018, new List<SwitchModel>());
            SwitchStatusList.Add(1019, new List<SwitchModel>());
            SwitchStatusList.Add(1020, new List<SwitchModel>());
            SwitchStatusList.Add(1021, new List<SwitchModel>());
            SwitchStatusList.Add(1022, new List<SwitchModel>());
            SwitchStatusList.Add(1023, new List<SwitchModel>());
            SwitchStatusList.Add(DeviceID.翻转机, new List<SwitchModel>());
            Dictionary<int, bool[]> SwitchList = new Dictionary<int, bool[]>();
            Dictionary<int, bool[]> PreSwitchList = new Dictionary<int, bool[]>();
            for (int k = 1001; k < 1024; k++)
            {
                SwitchList.Add(k, new bool[8]);
                PreSwitchList.Add(k, new bool[8]);
            }
            SwitchList.Add(DeviceID.翻转机, new bool[8]);
            PreSwitchList.Add(DeviceID.翻转机, new bool[8]);
  
                //采集程序
                Task.Run(new Action(() =>
            {
                WebSocketServiceHost serviceHost;
                webserver.WebSocketServices.TryGetServiceHost("/MES1", out serviceHost);
                while (true)
                {
                    Thread.Sleep(1000);
                    // 业务逻辑
                    List<SwitchModel> switchModels = new List<SwitchModel>();
                    for (int k = 1001; k < 1024; k++)
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            switchModels.Add(new SwitchModel
                            {
                                Id = k,
                                DeviceName = "等离子",
                                PassName = $"通道{i}",
                                Status = new Random().Next() % 2==0?true:false
                            });
                        }
                    }
                    //for (int k = 1001; k < 1024; k++)
                    //{
                    //    JObject obj1 = new JObject();
                    //    var res = NetFactory.redisPool.ReadHashKey("MTCD:SwitchGateWay", k.ToString());
                    //    if (res.IsSuccess)
                    //    {
                    //        JObject obj = JObject.Parse(res.Content);
                    //        if (obj.Value<bool>("IsActive"))
                    //        {
                    //            JArray array = JArray.Parse(obj["SwitchData"].ToString());
                    //            int i = 0;
                    //            foreach (var item in array)
                    //            {
                    //                SwitchList[k][i] = Convert.ToBoolean(((JObject)item)["Status"]);
                    //                switchModels.Add(new SwitchModel
                    //                {
                    //                    Id = k,
                    //                    DeviceName ="等离子",
                    //                    PassName = ((JObject)item)["PassName"].ToString(),
                    //                    Status = Convert.ToBoolean(((JObject)item)["Status"])
                    //                });
                    //                i++;
                    //            }

                    //            if (ServerInfo.CheckArray(SwitchList[k], PreSwitchList[k]))
                    //            {
                    //                if (serviceHost.Sessions.Sessions.Count() > 0)
                    //                {
                    //                    serviceHost.Sessions.Broadcast(JsonConvert.SerializeObject(switchModels));
                    //                }
                    //            }
                    //            PreSwitchList[k] = (bool[])SwitchList[k].Clone();
                    //        }

                    //    }
                    //}
                    //

                    if (serviceHost.Sessions.Sessions.Count() > 0)
                    {
                        serviceHost.Sessions.Broadcast(JsonConvert.SerializeObject(switchModels));
                    }
                }
            }));

            
        }




    }

        public class SwitchModel
        {
            public int Id { get; set; }
            public string DeviceName { get; set; }

            public string PassName { get; set; }

            public bool Status { get; set; }
        }
}
